class FireBall extends AcGameObject{
    constructor(playground, player,x,y,radius,vx,vy,color,speed,move_length,damage) {
        super();
        this.playground = playground;
        this.player = player
        this.ctx = this.playground.game_map.ctx;
        this.x = x;
        this.y = y;
        this.vx = vx;
        this.vy = vy;
        this.radius = radius;
        this.color = color;
        this.speend = speed;
        this.move_length = move_length;
        this.eps = 0.01;
        this.damage = damage;
        this.img = new Image();
        this.img.src = "https://app4251.acapp.acwing.com.cn/static/image/head_pic/Paimon.jpg"
    }
    start(){

    }
    update(){
        if(this.move_length < this.eps){
            this.destroy();
            return false;
        }
        this.update_move();
        if(this.player.character !== "enemy"){
            this.update_attack();
        }
        this.render();
    }
    update_move(){
        let moved = Math.min(this.move_length,this.speend * this.timedelta / 1000);
        this.x += this.vx * moved;
        this.y += this.vy * moved;
        this.move_length -= moved

    }

    update_attack(){
        for (let i = 0;i<this.playground.players.length;i++){
            let player = this.playground.players[i];
            if(this.player !== player && this.is_collision(player)){
                this.attack(player);
                break;
            }
        }

    }
    gei_dist(x1,y1,x2,y2){
        let dx = x1 - x2;
        let dy = y1 - y2;
        return Math.sqrt(dx * dx + dy * dy);
    }
    is_collision(player){
        let distance = this.gei_dist(this.x,this.y,player.x,player.y);
        if (distance < this.radius + player.radius)
            return true;
        return false;
    }
    attack(player){
        let angle = Math.atan2(player.y - this.y, player.x - player.x);
        player.is_attacked(angle,this.damage);

        if (this.playground.mode === "multi mode"){
            this.playground.mps.send_attack(player.uuid,player.x,player.y,angle,this.damage,this.uuid);
        }

        this.destroy();
    }

    render(){
        let scale = this.playground.scale;
        this.ctx.save();
        this.ctx.beginPath();
        this.ctx.arc(this.x * scale, this.y * scale, this.radius * scale, 0, Math.PI * 2, false);
        this.ctx.stroke();
        this.ctx.clip();
        this.ctx.drawImage(this.img, (this.x - this.radius)*scale, (this.y - this.radius)*scale, this.radius * 2 * scale, this.radius * 2 *scale);
        this.ctx.restore();
    }
    on_destroy() {
        let fireballs = this.player.fireballs;
        for (let i = 0;i<fireballs.length;i++){
            if (fireballs[i] === this){
                fireballs.splice(i,1);
                break;
            }
        }
    }
}